package nsoft.swing.engine;

import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import nsoft.swing.action.ApplicationEventListener;
import nsoft.swing.annotation.AButton;
import nsoft.swing.annotation.AComponent;
import nsoft.swing.annotation.APanel;
import nsoft.swing.annotation.Type;
import nsoft.util.Property;

public final class DefaultComponentFactory implements IComponentFactory {

	public JLabel createLabel(Property property, AComponent component) {

		if (component.type() != Type.LABEL)
			return null;
		JLabel label = new JLabel(property.getValueAsString());
		PropertySetter.setProperties(label, component.properties());
		return label;
	}

	public JButton createButton(final Property property, AButton buttonMetadata) {
		JButton button = new JButton(buttonMetadata.label());
		PropertySetter.setProperties(button, buttonMetadata.properties());
		// TODO To be refined.
		// new
		// ApplicationAction<ActionEvent>(buttonMetadata.label(),buttonMetadata.action(),property.getBean());
		button.addActionListener(new ApplicationEventListener<ActionEvent>(
				buttonMetadata.label(), buttonMetadata.action(), property
						.getBean()));
		return button;
	}

	public JComponent createComponent(Property property, AComponent component) {
		// TODO replace with switch in java 7.
		if (component.type() == Type.LABEL) {
			return createLabel(property, component);
		}
		return null;
	}

	public JPanel createPanel(APanel panelMetadata) {
		JPanel panel = new JPanel();
		PropertySetter.setProperties(panel, panelMetadata.properties());
		return new JPanel();
	}
}
